const { Sequelize } = require('sequelize');

const sequelize = new Sequelize({
  dialect: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: '888mei525',
  database: 'lingxi_system',
  logging: false
});

async function addOrderFields() {
  try {
    await sequelize.authenticate();
    console.log('数据库连接成功');
    
    // 添加采购人员姓名字段
    try {
      await sequelize.query("ALTER TABLE sales_orders ADD COLUMN purchaser_name VARCHAR(100) NULL DEFAULT NULL COMMENT '采购人员姓名'");
      console.log('添加 purchaser_name 字段成功');
    } catch (error) {
      if (error.message.includes('Duplicate column name')) {
        console.log('purchaser_name 字段已存在');
      } else {
        console.error('添加 purchaser_name 字段时出错:', error);
      }
    }
    
    // 添加采购单位ID字段
    try {
      await sequelize.query("ALTER TABLE sales_orders ADD COLUMN purchaser_organization_id BIGINT NULL DEFAULT NULL COMMENT '采购单位ID'");
      console.log('添加 purchaser_organization_id 字段成功');
    } catch (error) {
      if (error.message.includes('Duplicate column name')) {
        console.log('purchaser_organization_id 字段已存在');
      } else {
        console.error('添加 purchaser_organization_id 字段时出错:', error);
      }
    }
    
    // 添加采购单位名称字段
    try {
      await sequelize.query("ALTER TABLE sales_orders ADD COLUMN purchaser_organization_name VARCHAR(200) NULL DEFAULT NULL COMMENT '采购单位名称'");
      console.log('添加 purchaser_organization_name 字段成功');
    } catch (error) {
      if (error.message.includes('Duplicate column name')) {
        console.log('purchaser_organization_name 字段已存在');
      } else {
        console.error('添加 purchaser_organization_name 字段时出错:', error);
      }
    }
    
    // 添加供应商联系人姓名字段
    try {
      await sequelize.query("ALTER TABLE sales_orders ADD COLUMN supplier_name VARCHAR(100) NULL DEFAULT NULL COMMENT '供应商联系人姓名'");
      console.log('添加 supplier_name 字段成功');
    } catch (error) {
      if (error.message.includes('Duplicate column name')) {
        console.log('supplier_name 字段已存在');
      } else {
        console.error('添加 supplier_name 字段时出错:', error);
      }
    }
    
    // 添加供应商组织ID字段
    try {
      await sequelize.query("ALTER TABLE sales_orders ADD COLUMN supplier_organization_id BIGINT NULL DEFAULT NULL COMMENT '供应商组织ID'");
      console.log('添加 supplier_organization_id 字段成功');
    } catch (error) {
      if (error.message.includes('Duplicate column name')) {
        console.log('supplier_organization_id 字段已存在');
      } else {
        console.error('添加 supplier_organization_id 字段时出错:', error);
      }
    }
    
    // 添加供应商组织名称字段
    try {
      await sequelize.query("ALTER TABLE sales_orders ADD COLUMN supplier_organization_name VARCHAR(200) NULL DEFAULT NULL COMMENT '供应商组织名称'");
      console.log('添加 supplier_organization_name 字段成功');
    } catch (error) {
      if (error.message.includes('Duplicate column name')) {
        console.log('supplier_organization_name 字段已存在');
      } else {
        console.error('添加 supplier_organization_name 字段时出错:', error);
      }
    }
    
  } catch (error) {
    console.error('数据库操作时出错:', error);
  } finally {
    await sequelize.close();
  }
}

addOrderFields();